/**
 * 项目操作
 */

import  store from 'vuex';

import projectReq from "@/api/project.js";

import * as types from '../mutation-types'

const project={
    state:{

    },
    mutations:{

    },
    actions:{
      /**
       * 删除文件
       * @param commit
       * @param state
       * @param rootState
       * @param data
       * @returns {Promise}
       */
      doDeleteFile({commit,state,rootState},data){

        rootState.loading=!rootState.loading;

        return new Promise((resolve,reject) => {

          projectReq.deleteFile(data).then(response => {

            rootState.loading = !rootState.loading;

            resolve(response);

          },response => {

            rootState.loading = !rootState.loading;

            reject(response);

          }).catch(err =>{
            console.log(err);

            reject(err);
          });

        });

      },
      /**
       * 上传文件
       * @param commit
       * @param state
       * @param rootState
       * @param data
       * @returns {Promise}
       */
      doLoadFile({commit,state,rootState},data) {

        rootState.loading =!rootState.loading;

        return new Promise((resolve,reject) => {

          projectReq.loadFile(data).then(response => {

            rootState.loading = !rootState.loading;

            resolve(response);

          },response=>{

            rootState.loading = !rootState.loading;

            reject(response);

          }).catch(err=>{

            console.log(err);

          });

        });

      },
      /**
       * 创建项目
       * @param commit
       * @param state
       * @param rootState
       * @param data
       * @returns {Promise}
       */
      createProject:({commit,state,rootState},data)=>{

        rootState.loading =!rootState.loading;

        return new Promise((resolve,reject) => {

          projectReq.doCreateProject(data).then(response => {

            rootState.loading = !rootState.loading;

            resolve(response);

          },response=>{

            rootState.loading = !rootState.loading;

            reject(response);

          });

        });

      },
      /**
       * 编辑项目
       * @param commit
       * @param state
       * @param data
       * @returns {Promise}
       */
      editProject:({ commit, state,rootState},data)=>{

        rootState.loading=!rootState.loading;

        return new Promise((resolve,reject)=>{

          projectReq.doEditProject(data).then((res)=>{

            resolve(res);

            rootState.loading=!rootState.loading;

          },(res)=>{

            reject(res);

            rootState.loading=!rootState.loading;

          }).catch(err =>{

            reject(err);

            rootState.loading=!rootState.loading;

          });

        })
      },
      /**
       * 获取所有的项目集合
       * @param commit
       * @param state
       * @param data
       */
      getProjects:({commit,state,rootState})=>{

        rootState.loading =!rootState.loading;

        return new Promise((resolve,reject)=>{

          projectReq.doGetProject(rootState.user.token).then(res =>{

            rootState.loading =!rootState.loading;

            resolve(res);

          }, res =>{

            reject(res);

          });

        });

     },
      getSingeProject:({commit,state,rootState},data)=>{

        rootState.loading=!rootState.loading;

        return new Promise((resolve,reject)=>{

          projectReq.doGetSingeProject(data).then((res)=>{

            resolve(res);

            rootState.loading=!rootState.loading;


          },(res)=>{

            reject(res);

            rootState.loading=!rootState.loading;

          }).catch(err=>{

            this.$message('系统错误，创建项目失败!,错误信息:'+err);

            reject(err);

          })

        });

      },
      getProgressPlan({commit,state,rootState},data){

        rootState.loading=!rootState.loading;

        return new Promise((resolve,reject)=>{

            projectReq.doGetProgressPlan(data).then(res=>{

              rootState.loading=!rootState.loading;

              resolve(res);

            },res=>{

              reject(res);

            });

        });
      },
      getProjectState({ commit,state,rootState},data){
        rootState.loading=!rootState.loading;

        return new Promise((resolve,reject) => {

          projectReq.doGetProjectState(data).then(res=>{

            rootState.loading=!rootState.loading;

            resolve(res);

          },res =>{

            rootState.loading=!rootState.loading;

            reject(res);

          }).catch(err => {

            reject(res);

            rootState.loading=!rootState.loading;

          });

        });

      },
      /**
       * 新增项目
       * @param commit
       * @param state
       * @param rootState
       * @param data
       */
      addProjectBuildPlan({commit,state,rootState},data){

        rootState.loading=!rootState.loading;

        return new Promise((resolve,reject)=>{

          projectReq.doAddProjectBuildPlan(data).then(res =>{

            rootState.loading=!rootState.loading;

            resolve(res);

          },res => {

            rootState.loading=!rootState.loading;

            reject(res);

          }).catch(err =>{

            rootState.loading=!rootState.loading;

            reject(res);

          });

        });

      },
      /**
       * 提交用户的检查计划
       * @param commit
       * @param state
       * @param rootState
       * @param data
       * @returns {Promise}
       */
      addSendCheckPlan({commit,state,rootState},data){

        rootState.loading=!rootState.loading;

        return new Promise((resolve,reject)=>{

          projectReq.doSendCheckPlan(data).then(res =>{

            rootState.loading=!rootState.loading;

            resolve(res);

          },res => {

            rootState.loading=!rootState.loading;

            reject(res);

          }).catch(err =>{

            rootState.loading=!rootState.loading;

            reject(res);

          });

        });

      },
      modifyBuildPlan({commit,state,rootState},data){

        rootState.loading=!rootState.loading;

        return new Promise((resolve,reject)=>{

          projectReq.doModifyBuildPlan(data).then(res =>{

            rootState.loading=!rootState.loading;

            resolve(res);

          },res => {

            rootState.loading=!rootState.loading;

            reject(res);

          }).catch(err =>{

            rootState.loading=!rootState.loading;

            reject(res);

          });

        });

      },
      /**
       * 获取check_plan_id
       * @param commit
       * @param state
       * @param rootState
       * @param data
       * @returns {Promise}
       */
      getTaskInfo({commit,state,rootState},data){

        rootState.loading=!rootState.loading;

        return new Promise((resolve,reject)=>{

          projectReq.doGetTaskInfo(data).then(res =>{

            rootState.loading=!rootState.loading;

            resolve(res);

          },res => {

            rootState.loading=!rootState.loading;

            reject(res);

          }).catch(err =>{

            rootState.loading=!rootState.loading;

            reject(res);

          });

        });

      },
      getCheckPlanDetail({commit,state,rootState},data){

        rootState.loading=!rootState.loading;

        return new Promise((resolve,reject)=>{

          projectReq.getCheckPlanDetail(data).then(res =>{

            rootState.loading=!rootState.loading;

            resolve(res);

          },res => {

            rootState.loading=!rootState.loading;

            reject(res);

          }).catch(err =>{

            rootState.loading=!rootState.loading;

            reject(res);

          });

        });

      }
    }
}

export default project;
